package com.jimikongjian.controller;

import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.RoomFollower;
import com.jimikongjian.service.RoomFollowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by fuxiuyang on 17/3/14.
 */
@RestController("roomFollowerController")
@RequestMapping(value = "/v1")
public class RoomFollowerController {


    @Autowired
    @Qualifier("roomFollowerService")
    private RoomFollowerService roomFollowerService;


    /**
     * 关注某一个空间
     * @param roomFollower
     * @return
     */
    @RequestMapping(value = "/room_follower", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage followerRoom(@RequestBody RoomFollower roomFollower){
        return roomFollowerService.followerRoom(roomFollower);
    }


    /**
     * 查询某一空间内是否含有该用户
     * @param userId
     * @param roomId
     * @return
     */
    @RequestMapping(value = "/room_follower", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage hasUserByUserIdAndRoomId(@RequestParam(name = "userId", defaultValue = "-1") Integer userId,
                                                    @RequestParam(name = "roomId", defaultValue = "-1") Integer roomId){
        return roomFollowerService.hasUserByUserIdAndRoomId(userId, roomId);
    }

    /**
     * 查询某一空间内所有的成员
     * @param userId
     * @param roomId
     * @return
     */
    @RequestMapping(value = "/room_members", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage getRoomMembers(@RequestParam(name = "start", defaultValue = "0") Integer start,
                                          @RequestParam(name = "length", defaultValue = "10") Integer length,
                                          @RequestParam(name = "userId", defaultValue = "-1") Integer userId,
                                          @RequestParam(name = "roomId", defaultValue = "-1") Integer roomId){
        return roomFollowerService.selectRoomFollowerByRoomId(roomId, userId, start, length);
    }


    /**
     * 删除该空间的某一个关注者
     * @param map
     * @return
     */
    @RequestMapping(value = "/room_follower", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseMessage deleteRoomFollower(@RequestBody Map<String,String> map){
        int userId = -1;
        int roomId = -1;
        int operator = -1;
        if(map.containsKey("userId")){
            userId = Integer.parseInt(map.get("userId"));
        }
        if(map.containsKey("roomId")){
            roomId = Integer.parseInt(map.get("roomId"));
        }
        if(map.containsKey("operator")){
            operator = Integer.parseInt(map.get("operator"));
        }
        return roomFollowerService.deleteRoomFollower(userId, roomId, operator);
    }
}
